package com.chenxin.wms.process.outbound.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.chenxin.common.constant.WmsConstants;
import com.chenxin.common.core.domain.ProcResult;
import com.chenxin.common.enums.wms.ObDetailStatus;
import com.chenxin.common.enums.wms.ObMasterStatus;
import com.chenxin.common.enums.wms.ObMasterType;
import com.chenxin.common.exception.ServiceException;
import com.chenxin.common.exception.wms.WmsException;
import com.chenxin.common.utils.StringUtils;
import com.chenxin.wms.domain.ObDetailEntity;
import com.chenxin.wms.domain.ObMasterEntity;
import com.chenxin.wms.mapper.ObDetailMapper;
import com.chenxin.wms.mapper.ObMasterMapper;
import com.chenxin.wms.process.outbound.IProcObDetailExecutor;
import com.chenxin.wms.process.outbound.IProcStockPrepareByDetail;
import com.chenxin.wms.process.outbound.IProcWorkOrderOut;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class ProcWorkOrderOut implements IProcWorkOrderOut {
    @Autowired
    private ObMasterMapper obMasterMapper;
    @Autowired
    private ObDetailMapper obDetailMapper;
    @Autowired
    private IProcStockPrepareByDetail procStockPrepareByDetail;

    @Transactional
    @Override
    public ProcResult call(Long obMasterId, Long creator) {
        if (StringUtils.isNull(obMasterId)) {
            return ProcResult.error("work_order_out_err001", "主单ID不能为空。");
        }
        try {
            // 获取主单详情
            ObMasterEntity obMaster = obMasterMapper.selectById(obMasterId);
            if (obMaster == null) {
                return ProcResult.error("work_order_out_err002", "主单ID【" + obMasterId + "】不存在。");
            }
            String workOrderNo = obMaster.getWorkOrderNo();
            int obMasterStatus = obMaster.getObMasterStatus();
            String obMasterType = obMaster.getObMasterType();
            // 已备货完成的工单无需再次备货出库
            if (obMasterStatus > ObMasterStatus.PREPARED.getCode()) {
                return ProcResult.error("work_order_out_err003", "主单【" + obMasterId + "】状态为【" + obMasterStatus + "】，不能备货出库。");
            }
            // 只有车间叫料工单类型才能作业、
            if (!ObMasterType.CALL_WIP.getCode().equals(obMasterType)) {
                return ProcResult.error("work_order_out_err004", "主单【" + obMasterId + "】类型为【" + obMasterType + "】，不能备货出库。");
            }
            // 根据主单ID获取叫料明细信息
            QueryWrapper<ObDetailEntity> obDetailsQW = new QueryWrapper<>();
            obDetailsQW.eq("ob_master_id", obMasterId)
                    .lt("ob_detail_status", ObDetailStatus.PREPARED.getCode());
            List<ObDetailEntity> obDetails = obDetailMapper.selectList(obDetailsQW);
            if (obDetails == null || obDetails.size() == 0) {
                return ProcResult.error("work_order_out_err005", "主单【" + obMasterId + "】无可备货出库的叫料明细。");
            }
            // 循环处理出库明细
            for (ObDetailEntity prepareDetail : obDetails) {
                ProcResult prepareResult = procStockPrepareByDetail.call(prepareDetail.getObDetailId(), creator);
                if (!WmsConstants.PROC_SUCCESS.equals(prepareResult.getCode())) {
                    throw new WmsException("work_order_out_err006", prepareResult.getMessage());
                }
            }
            // 备货完成后执行出库流程

            return ProcResult.success("工单【" + workOrderNo + "】发料执行成功。");
        } catch (WmsException e) {
            throw new WmsException("work_order_out_error", "单据【" + obMasterId + "】发料异常，causeBy:" + e.getMessage());
        }
    }
}
